#ifdef WIN32
#include <windows.h>
#endif

#include "VirtualCameraCommunication.h"
#include <GL/glut.h>
#include <GL/gl.h>

// Pretty windows specific stuff - shared memory inter process communication
#ifdef WIN32

#define BUF_SIZE 921600
TCHAR szName[]=TEXT("ARCameraFMO");
HANDLE hMapFile = NULL;
unsigned char* pBuf = NULL;

int sendPixelsToSharedMemory(int width, int height)
{
	if (pBuf == NULL)
	{
		hMapFile = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, BUF_SIZE, szName);
 
		if (hMapFile == NULL) 
			return 1;

		pBuf = (unsigned char*) MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, BUF_SIZE);           
 
		if (pBuf == NULL) 
		{ 
			CloseHandle(hMapFile);
			return 2;
		}
	}

	glReadPixels(0, 0, width, height, 0x80E0, GL_UNSIGNED_BYTE, (unsigned char*) pBuf); //GL_BGR_EXT replaced with 0x80E0 

	return 0;
}

void freeSharedMemory()
{
	if (pBuf != NULL)
		UnmapViewOfFile(pBuf);

	if (hMapFile != NULL)
		CloseHandle(hMapFile);
}

#else

int sendPixelsToSharedMemory(int width, int height)
{
	return 0;
}

void freeSharedMemory()
{

}

#endif
